//
//  UIAlertController+JF.swift
//  jianfeiApp2C
//
//  Created by Mac on 2020/7/7.
//  Copyright © 2020 com.yanjiuyuan. All rights reserved.
//

import UIKit

extension UIAlertController {
    /// 显示AlertView
    ///
    /// - Parameters:
    ///   - title: 标题
    ///   - message: 信息
    ///   - cancelTitle: 取消按钮标题
    ///   - okTitle: 确定按钮
    class func showAlertView(title: String?, message: String?, cancelTitle: String?, okTitle: String, callback: @escaping ((_ index: Int)->())) {
        
        guard let message = message else {
            return
        }
        
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        
        if let cancelTitle = cancelTitle {
            let cancelAction = UIAlertAction(title: cancelTitle, style: .cancel) { (action) in
                callback(0)
            }
            
            alert.addAction(cancelAction)
        }
        
        let okAction = UIAlertAction(title: okTitle, style: .default) { (action) in
            callback(1)
        }
        
        alert.addAction(okAction)
        
        DispatchQueue.main.async {
            UIViewController.topMostVC?.present(alert, animated: true, completion: nil)
        }
    }
    
    class func showActionSheet(title:String?, message: String?, cancelTitle:String?, cancelBtnCallBack: (((UIAlertAction) -> Void)? ), doneTitle:String?, doneBtnCallBack: (((UIAlertAction) -> Void)? )) {
           
        let alert = UIAlertController.init(title: title ?? "操作选择", message: message ?? "", preferredStyle: UIAlertController.Style.actionSheet)
        
        if cancelTitle != nil {
            let cancelAction = UIAlertAction.init(title: cancelTitle ?? "", style: UIAlertAction.Style.default, handler: cancelBtnCallBack)
             alert.addAction(cancelAction)
        }
        
        if doneTitle != nil {
            let doneAction = UIAlertAction.init(title: doneTitle ?? "", style: UIAlertAction.Style.default, handler: doneBtnCallBack)
            alert.addAction(doneAction)
        }
        
        let cancel = UIAlertAction.init(title: "取消", style: UIAlertAction.Style.cancel, handler: nil)
           
        alert.addAction(cancel)
        UIViewController.topMostVC?.present(alert, animated: true, completion: nil)
    }
}
